-- *************************************************************************
-- * TitanNameToggle.lua for Version 3.0 r1
-- * by Joejanko (MNyteTemplar @ Undermine)
-- *************************************************************************
-- * This plugin displays in a tooltip the current Names Interface settings
-- * for Friendly Players, Guilds, Titles, Friendly Pets & Minions, Friendly
-- * Creations, Companions, and NPCs. It also allows you to toggle those 
-- * settings on and off from the Titan Panel with only a few mouse clicks.
-- *
-- * Acknowledgements: 
-- *    Blueforge, the original author of the mod (versions 1.0 and 2.0)
-- *    Tristanian and HonorGoG, who helped with some of the programming
-- *    Poena @ Area 52, who's Titan Gold Tracker showed me how to 
-- *                     document the code
-- *    Sensity, who corrected my German localization
-- *    oXid_FoX, who corrected my French localization
-- *    [Your Name Here!], who corrected my Spanish localization
-- *
-- * I'm looking for others to help with additional localization.
-- *************************************************************************

-- ******************************* Constants *******************************
TITAN_NAMETOGGLE_ID = "NameToggle";
TITAN_NAMETOGGLE_VERSION = "3.0 r1";
-- *************************************************************************

-- ******************************* Functions *******************************

-- *************************************************************************
-- NAME : TitanPanelNameToggleButton_OnLoad()
-- DESC : Registers the plugin upon it loading and identifies events. 
--        In this case, the event is an update of a console variable.
--        Also, an entry is made to the chat log when entering world.
--        Finally, the plugin is added to a table of right-side plugins.
--        This allows you to swap its position on the Titan bar only with
--        plugins in the table (right-side plugins).
-- *************************************************************************

function TitanPanelNameToggleButton_OnLoad(self)
	    if( DEFAULT_CHAT_FRAME ) then
			DEFAULT_CHAT_FRAME:AddMessage(GREEN_FONT_COLOR_CODE.. "Titan Panel [NameToggle] v3.0 r1 by Joejanko (MNyteTemplar on Undermine)");
	end
	self.registry = { 
		    id = TITAN_NAMETOGGLE_ID,
		    version = TITAN_NAMETOGGLE_VERSION,
            menuText = "Name Toggle", 
		    category = "Interface",
            tooltipTitle = "Name Toggle Info";
		    tooltipTextFunction = "TitanPanelNameToggleButton_GetTooltipText", 
            icon = "Interface\\GossipFrame\\BinderGossipIcon.blp",
		    iconWidth = 16,
		    iconButtonWidth = 16,	 
	};
    
    self:RegisterEvent("CVAR_UPDATE");
    
    tinsert(TITAN_PANEL_NONMOVABLE_PLUGINS, TITAN_NAMETOGGLE_ID)
end

-- *************************************************************************
-- NAME : TitanPanelNameToggleButton_OnEvent()
-- DESC : This section will grab the events registered to the plugin and
--        act on them.  In this case, the event is an update of a console
--        variable, specifically the names of Friendly Players, Guilds,
--        Titles, Friendly Pets & Minions, Friendly Creations, Companions,
--        and NPCs.  If any of those settings are changed, the plugin's
--        tooltip is updated.
-- *************************************************************************

function TitanPanelNameToggleButton_OnEvent(self, event, arg1, ...)
    if (event == "CVAR_UPDATE") then
        if (arg1 == "UnitNameFriendlyPlayerName" or arg1 == "UnitNamePlayerGuild" or arg1 == "UnitNamePlayerPVPTitle" or arg1 == "UnitNameFriendlyPetName" or arg1 == "UnitNameFriendlyCreationName" or arg1 == "UnitNameCompanionName" or arg1 == "UnitNameNPC") then
            TitanPanelButton_UpdateTooltip(self);
        end
    end
end

-- *************************************************************************
-- Name: TitanPanelNameToggleButton_GetTooltipText()
-- Desc: Gets the tooltip text, what appears when you hover over the icon
--       on the Titan Panel.  In this case, it displays each of the settings
--       that can be toggled and whether a setting is on or off.  Below the
--       settings is Hint text.  The file Localization.lua contains the
--       specific text strings.
-- *************************************************************************

function TitanPanelNameToggleButton_GetTooltipText()
    local text = "";
    
    text = text .. TitanUtils_GetGreenText(TOOLTIPTEXT_HINT1);

	text = text .."\n".. TOOLTIPTEXT_LINE1;
    if (GetCVar("UnitNameFriendlyPlayerName") == "1") then
        text = text .."\t".. TitanUtils_GetGreenText(TOOLTIPTEXT_GREENTEXT);
    else
        text = text .."\t".. TitanUtils_GetRedText(TOOLTIPTEXT_REDTEXT);
    end

    text = text .."\n".. TOOLTIPTEXT_LINE2;
    if (GetCVar("UnitNamePlayerGuild") == "1") then
        text = text .."\t".. TitanUtils_GetGreenText(TOOLTIPTEXT_GREENTEXT);
    else
        text = text .."\t".. TitanUtils_GetRedText(TOOLTIPTEXT_REDTEXT);
    end
    
    text = text .."\n".. TOOLTIPTEXT_LINE3;
    if (GetCVar("UnitNamePlayerPVPTitle") == "1") then
        text = text .."\t".. TitanUtils_GetGreenText(TOOLTIPTEXT_GREENTEXT);
    else
        text = text .."\t".. TitanUtils_GetRedText(TOOLTIPTEXT_REDTEXT);
    end

    text = text .."\n\n".. TitanUtils_GetGreenText(TOOLTIPTEXT_HINT2);

    text = text .."\n".. TOOLTIPTEXT_LINE4;
    if (GetCVar("UnitNameFriendlyPetName") == "1") then
        text = text .."\t".. TitanUtils_GetGreenText(TOOLTIPTEXT_GREENTEXT);
    else
        text = text .."\t".. TitanUtils_GetRedText(TOOLTIPTEXT_REDTEXT);
    end

    text = text .."\n".. TOOLTIPTEXT_LINE5;
    if (GetCVar("UnitNameFriendlyCreationName") == "1") then
        text = text .."\t".. TitanUtils_GetGreenText(TOOLTIPTEXT_GREENTEXT);
    else
        text = text .."\t".. TitanUtils_GetRedText(TOOLTIPTEXT_REDTEXT);
    end
    
    text = text .."\n".. TOOLTIPTEXT_LINE6;
    if (GetCVar("UnitNameCompanionName") == "1") then
        text = text .."\t".. TitanUtils_GetGreenText(TOOLTIPTEXT_GREENTEXT);
    else
        text = text .."\t".. TitanUtils_GetRedText(TOOLTIPTEXT_REDTEXT);
    end

    text = text .."\n\n".. TitanUtils_GetGreenText(TOOLTIPTEXT_HINT3);

    text = text .."\n".. TOOLTIPTEXT_LINE7;
    if (GetCVar("UnitNameNPC") == "1") then
        text = text .."\t".. TitanUtils_GetGreenText(TOOLTIPTEXT_GREENTEXT);
    else
        text = text .."\t".. TitanUtils_GetRedText(TOOLTIPTEXT_REDTEXT);
    end    

    text = text .."\n\n".. TitanUtils_GetGreenText(TOOLTIPTEXT_HINT4);
    return text;
end

-- *************************************************************************
-- Name: TitanPanelNameToggleButton_OnClick()
-- Desc: Determines what happens when you click the NameToggle icon. 
--       In this case, it sets a shift-left-click to toggle the names of
--       Friendly Players, Guilds, and Titles, a control-left-click to 
--       toggle Friendly Pets & Minions, Friendly Creations, and Companions,
--       and an alt-left-click to toggle NPC Names. It sets a right-click to 
--       hide the tooltip and display the right-click menu. It updates the
--       tooltip after each click.
-- *************************************************************************

function TitanPanelNameToggleButton_OnClick(self,button)
    if (button == "LeftButton" and IsShiftKeyDown()) then
                                               -- A shift-left-click affects
    	local ufplayname = GetCVar("UnitNameFriendlyPlayerName");
                                                  -- Friendly Players names,
        local uguild = GetCVar("UnitNamePlayerGuild");       -- Guild names,
        local utitle = GetCVar("UnitNamePlayerPVPTitle"); -- and PVP titles.
        
        if (ufplayname == "0") then           -- If Friendly Players is off,
            SetCVar("UnitNameFriendlyPlayerName", "1")
                                               -- turns on Friendly Players,
            SetCVar("UnitNamePlayerGuild", "0")         -- turns off Guilds,
            SetCVar("UnitNamePlayerPVPTitle", "0")  -- and turns off Titles.
        else                               -- But if Friendly Players is on,
            if (uguild == "0") then                 -- and if Guilds is off,
                SetCVar("UnitNamePlayerGuild", "1")      -- turns on Guilds.
            else               -- But if Friendly Players and Guilds are on,
                if (utitle == "0") then                -- and Titles is off,
                    SetCVar("UnitNamePlayerGuild", "0") -- turns off Guilds,
                    SetCVar("UnitNamePlayerPVPTitle", "1") 
                                                     -- and turns on Titles.
                else                                 -- But if all 3 are on,
                    SetCVar("UnitNameFriendlyPlayerName", "0")      
                                              -- turns off Friendly Players,
                    SetCVar("UnitNamePlayerGuild", "0")           -- Guilds,
                    SetCVar("UnitNamePlayerPVPTitle", "0")    -- and Titles.
                end
            end
        end
	elseif (button == "LeftButton" and IsControlKeyDown()) then
                                             -- A control-left-click affects
        local ufpetname = GetCVar("UnitNameFriendlyPetName");
                                           -- Friendly Pets & Minions names,
        local ufcreatename = GetCVar("UnitNameFriendlyCreationName");  
                                                -- Friendly Creations names,
        local ucompname = GetCVar("UnitNameCompanionName"); 
                                                    -- and Companions names.
        
        if (ufpetname == "0") then     -- If Friendly Pets & Minions is off,
            SetCVar("UnitNameFriendlyPetName", "1")
                                        -- turns on Friendly Pets & Minions,
            SetCVar("UnitNameFriendlyCreationName", "0")         
                                            -- turns off Friendly Creations,
            SetCVar("UnitNameCompanionName", "0")
                                                -- and turns off Companions.
        else                        -- But if Friendly Pets & Minions is on,
            if (ufcreatename == "0") then
                                        -- and if Friendly Creations is off,
                SetCVar("UnitNameFriendlyCreationName", "1")      
                                             -- turns on Friendly Creations.
            else     -- But if Friendly Pets & Minions and Creations are on,
                if (ucompname == "0") then         -- and Companions is off,
                    SetCVar("UnitNameFriendlyCreationName", "0") 
                                            -- turns off Friendly Creations,
                    SetCVar("UnitNameCompanionName", "1") 
                                                 -- and turns on Companions.
                else                                 -- But if all 3 are on,
                    SetCVar("UnitNameFriendlyPetName", "0")      
                                       -- turns off Friendly Pets & Minions,
                    SetCVar("UnitNameFriendlyCreationName", "0")         
                                                      -- Friendly Creations,
                    SetCVar("UnitNameCompanionName", "0") -- and Companions.
                end
            end
        end
    elseif (button == "LeftButton" and IsAltKeyDown()) then
                                               -- An alt-left-click affects
        local unpc = GetCVar("UnitNameNPC");       -- NPC Names.
        if (unpc == "0") then                      -- if NPC Names is off,
            SetCVar("UnitNameNPC", "1");              -- it turns them on
        else                                       -- if NPC Names is on
            SetCVar("UnitNameNPC", "0");              -- it turns them off
        end
	elseif (button == "RightButton") then      -- A right-click
		GameTooltip:Hide();                    -- hides the tooltip and
		TitanPanelRightClickMenu_Toggle(self); -- displays the right-click menu.
    end
    TitanPanelButton_UpdateTooltip(self);     -- After a click, updates tooltip.
end

-- *************************************************************************
-- Name: TitanPanelRightClickMenu_PrepareNameToggleMenu()
-- Desc: Adds the title and the Hide command to the right-click menu.
-- *************************************************************************

function TitanPanelRightClickMenu_PrepareNameToggleMenu()
	TitanPanelRightClickMenu_AddTitle(TitanPlugins[TITAN_NAMETOGGLE_ID].menuText);
	TitanPanelRightClickMenu_AddCommand(TITAN_PANEL_MENU_HIDE, TITAN_NAMETOGGLE_ID, TITAN_PANEL_MENU_FUNC_HIDE);
end
